package com.sojoys.artifact.third.activemq;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.UUID;

/**
 * ActiveMQ 代理（用于创建 ActiveMQ 服务代理）
 *
 * @author huangyong
 * @since 1.0.0
 */
public class ActiveMQProxy {
	
//	private static final Logger LOGGER = LoggerFactory.getLogger(ActiveMQProxy.class);
	
	private static final ActiveMQProxy DEFAULT = new ActiveMQProxy();

	public static ActiveMQProxy me() {
		return DEFAULT;
	}
	
    

    private ActiveMQSender activeMQSender;
    
    
    public void setActiveMQSender(ActiveMQSender activeMQSender) {
		this.activeMQSender = activeMQSender;
	}

	private ActiveMQProxy() {
    	
	}

	/**
     * 创建一个接口
     * @param interfaceClass
     * @param destination
     */
//    public <T> T create(final Class<?> interfaceClass) {
//    	return create(interfaceClass, null);
//    }

    /**
     * 创建一个接口
     * @param interfaceClass
     * @param destination
     */
    @SuppressWarnings("unchecked")
	public <T> T  create(final Class<?> interfaceClass, final String destination,final boolean topic) {
        // 创建动态代理对象
    	return (T) Proxy.newProxyInstance(
                interfaceClass.getClassLoader(),
                new Class<?>[]{interfaceClass},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        // 创建 RPC 请求对象并设置请求属性
                    	ActiveMQRequest msg = new ActiveMQRequest();
                        msg.setId(UUID.randomUUID().toString());
                        msg.setInterfaceName(method.getDeclaringClass().getName());
                        msg.setMethodName(method.getName());
                        msg.setParameterTypes(method.getParameterTypes());
                        msg.setParameters(args);
                        msg.setReceive(false);
//                        if (destination==null) {
//                        	activeMQSender.convertAndSend(msg);
//						}else {
							activeMQSender.convertAndSend(destination, msg ,topic);
//						}
                        return null;
                    }
                }
        );
    }
    
    
    /**
     * 发送并接收
     * @param interfaceClass
     * @return
     */
    public <T> T createReceive(final Class<?> interfaceClass) {
        return createReceive(interfaceClass, null);
    }

    /**
     * 发送并接收
     * @param interfaceClass
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T createReceive(final Class<?> interfaceClass, final String destination) {
        // 创建动态代理对象
        return (T) Proxy.newProxyInstance(
                interfaceClass.getClassLoader(),
                new Class<?>[]{interfaceClass},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        // 创建 RPC 请求对象并设置请求属性
                    	ActiveMQRequest msg = new ActiveMQRequest();
                        msg.setId(UUID.randomUUID().toString());
                        msg.setInterfaceName(method.getDeclaringClass().getName());
                        msg.setMethodName(method.getName());
                        msg.setParameterTypes(method.getParameterTypes());
                        msg.setParameters(args);
                        msg.setReceive(true);
//                        if (destination==null) {
//                        	return activeMQSender.sendAndReceive(msg);
//						}else {
                        try {
                        	return activeMQSender.sendAndReceive(destination, msg);
						} catch (Exception e) {
							e.printStackTrace();
							// 回调异常处理
							throw e.getCause();
						}
//						}
                    }
                }
        );
    }
}
